<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>星辰诞愿 - 修改信息</title>
    <style>
        /* Inherit all styles from home.html */
        body {
            margin: 0;
            padding: 0;
            font-family: 'Comic Sans MS', cursive, sans-serif;
            background: linear-gradient(135deg, #fff5f5 0%, #fff0f5 100%);
            min-height: 100vh;
        }
        .nav {
            display: flex;
            align-items: center;
            padding: 15px 30px;
            background: rgba(255,255,255,0.95);
            box-shadow: 0 2px 15px rgba(210, 51, 105, 0.1);
            position: fixed;
            width: 100%;
            top: 0;
            z-index: 1000;
            border-bottom: 3px solid #ff8e9e;
        }
        .nav img {
            height: 50px;
            margin-right: 15px;
        }
        .blog-title {
            font-size: 24px;
            font-weight: bold;
            color: #d23369;
            text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
            position: absolute;
            left: 100px;
        }
        .nav-links {
            display: flex;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(255,255,255,0.9);
            padding: 5px 15px;
            border-radius: 30px;
            border: 2px solid #ff8e9e;
            box-shadow: 0 2px 10px rgba(210, 51, 105, 0.2);
        }
        .nav-span {
            margin: 0 10px;
            color: #d23369;
            text-decoration: none;
            font-weight: bold;
            padding: 8px 15px;
            border-radius: 20px;
            transition: all 0.3s;
            border: 2px solid transparent;
        }
        .nav-span:hover {
            background-color: #ffebee;
            color: #ff4081;
            border-color: #ff8e9e;
        }
        .space {
            flex-grow: 1;
        }
        .container {
            display: flex;
            justify-content: center;
            padding-top: 90px;
            min-height: calc(100vh - 90px);
            box-sizing: border-box;
        }
        .modify-container {
            width: 50%;
            padding: 20px;
        }
        .card {
            background: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 20px;
            box-shadow: 0 5px 15px rgba(210, 51, 105, 0.1);
            border: 1px solid #ffd6de;
            position: relative;
        }
        .card:before {
            content: "🎀";
            position: absolute;
            top: -10px;
            left: -10px;
            font-size: 20px;
        }
        h1 {
            color: #d23369;
            text-align: center;
            margin-bottom: 30px;
        }
        .avatar-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 20px;
        }
        .avatar-preview {
            width: 120px;
            height: 120px;
            border-radius: 25%;
            border: 3px solid #ff8e9e;
            object-fit: cover;
            margin-bottom: 15px;
        }
        .upload-btn {
            padding: 8px 15px;
            background: linear-gradient(to right, #a18cd1, #fbc2eb);
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-weight: bold;
            transition: all 0.3s;
        }
        .upload-btn:hover {
            background: linear-gradient(to right, #fbc2eb, #a18cd1);
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #d23369;
            font-weight: bold;
        }
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 2px solid #ffb8c6;
            border-radius: 8px;
            font-size: 16px;
            box-sizing: border-box;
        }
        .form-group input:focus {
            outline: none;
            border-color: #d23369;
        }
        .submit-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(to right, #ff6b88, #ff8e53);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 10px;
        }
        .submit-btn:hover {
            background: linear-gradient(to right, #ff8e53, #ff6b88);
        }
    </style>
</head>
<body>
    <div class="nav">
        <img src="pic/cat.jpg" alt="小猫" style="height: 50px;margin-right: 15px">
        <span class="blog-title">星辰诞愿 - 修改信息</span>
        <div class="nav-links">
            <a class="nav-span" href="home.html">主页</a>
            <a class="nav-span" onclick="logout()">注销</a>
        </div>
        <div class="space"></div>
    </div>
    <div class="container">
        <div class="modify-container">
            <div class="card">
                <h1>🎂 修改个人信息 🎂</h1>
                <div class="avatar-container">
                    <img id="avatar-preview" class="avatar-preview" src="" alt="用户头像">
                    <input type="file" id="avatar-upload" accept="image/*" style="display: none;">
                    <button class="upload-btn" onclick="document.getElementById('avatar-upload').click()">更换头像</button>
                </div>
                <div class="form-group">
                    <label for="userName">用户名</label>
                    <input type="text" id="userName" placeholder="请输入用户名">
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" placeholder="请输入新密码">
                </div>
                <div class="form-group">
                    <label for="birthday">生日</label>
                    <input type="text" id="birthday" placeholder="请输入生日,例如(2007-09-09),格式错误无法显示">
                </div>
                <button class="submit-btn" onclick="updateUserInfo()">保存修改</button>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
    <script>
        // Load current user info
        function loadUserInfo() {
            $.ajax({
                type: "get",
                url: "/user/getLoginUser",
                success: function(result) {
                    if (result.code === "SUCCESS") {
                        let user = result.data;
                        $("#userName").val(user.userName);
                        $("#birthday").val(user.birthdayStatus);
                    } else {
                        alert("加载用户信息失败: " + result.errorMessage);
                    }
                },
                error: function(error) {
                    if (error.status === 401) {
                        location.href = "login.html";
                    } else {
                        alert("网络错误，请稍后重试");
                    }
                }
            });
        }
        //Handle avatar upload
        document.getElementById('avatar-upload').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                // 先显示本地预览
                const reader = new FileReader();
                reader.onload = function(event) {
                    $("#avatar-preview").attr("src", event.target.result);
                    // 然后上传到服务器
                    uploadAvatar(file);
                }
                reader.readAsDataURL(file);
            }
        });
        // 上传头像到服务器
        function uploadAvatar(file) {
            const formData = new FormData();
            formData.append("file", file);
            $.ajax({
                type: "post",
                url: "/user/upload",
                data: formData,
                contentType: false,
                processData: false,
                success: function(result) {
                    if (result.code === "SUCCESS") {
                        downloadUserFile("#avatar-preview");
                        alert("头像上传成功!");
                    } else {
                        alert("头像上传失败: " + result.errorMessage);
                    }
                },
                error: function(error) {
                    alert("图片不得大于1MB");
                    console.error(error);
                }
            });
        }
        //保存修改信息
        function updateUserInfo() {
            const userName = $("#userName").val().trim();
            const password = $("#password").val().trim();
            const birthday = $("#birthday").val();
            if (!userName) {
                alert("用户名不能为空");
                return;
            }
            if (!password) {
                alert("密码不能为空");
                return;
            }
            if (!birthday) {
                alert("请选择生日日期");
                return;
            }
            const formData = new FormData();
            formData.append("userName", userName);
            formData.append("password", password);
            formData.append("birthday", birthday);
            $.ajax({
                type: "post",
                url: "/user/update",
                data: formData,
                contentType: false,
                processData: false,
                success: function(result) {
                    if (result.code === "SUCCESS") {
                        alert("信息更新成功!");
                    } else {
                        alert("更新失败: " + result.errorMessage);
                    }
                },
                error: function(error) {
                    if (error.status === 401) {
                        location.href = "login.html";
                    } else {
                        alert("网络错误，请稍后重试");
                    }
                }
            });
        }
        // Initial load
        $(document).ready(function() {
            loadUserInfo();
            downloadUserFile("#avatar-preview");
        });
        //注销
        function logout() {
            //删除token
            if(confirm("确定注销吗?")){
                $.ajax({
                    type: "post",
                    url:"user/cancel",
                    success:function (result){
                        if (result.code === "SUCCESS" && result.data === "注销成功"){
                            alert("注销成功")
                            localStorage.removeItem("user_token");
                            location.href = "/login.html";
                        }else {
                            alert(result.errorMessage);
                        }
                    },
                    error: function (error) {
                        if (error != null && error.status === 401) {
                            location.href = "login.html";
                        }
                    }
                })
            }
        }
    </script>
</body>
</html>